/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.faces.component.UIComponent;
import com.jb.web.bean.Page;
import com.jb.web.bean.Text;
import com.jb.web.bean.event.UIEvent;
import com.jb.web.bean.event.UIValueChangeEvent;
import com.jb.web.context.WebContext;
import com.jb.web.util.Filer;

/**
 * Renders a editable paragraph.
 */
public class ParagraphRenderer extends DefaultRenderer {
    public void onSave(WebContext pCtx, UIEvent event) {
        UIValueChangeEvent changeEvent = (UIValueChangeEvent)event;
        Text comp = (Text)changeEvent.getSource();

        String value = comp.getValue();

        String newValue = (String)changeEvent.getNewValue();

        if (value.startsWith("file:")) {
            String fileName = value.substring(5);
            fileName = evaluate(pCtx, comp, fileName);
            fileName = pCtx.getWebTop() + fileName;
            Filer.save(fileName, newValue);
            changeEvent.setChangeDenied(true);
        } else {
            comp.setValue(newValue);
        }
    }

    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        StringBuffer html = new StringBuffer("");
        Text hItem = (Text)itemToRender;

        if (!isVisible(pCtx, hItem)) {
            return html;
        }

        html.append("<div").append(getEditScript(pCtx, hItem));
        html.append(addTagProperty("class", hItem.getStyleClass(), null));
        html.append(addTagProperty("style", hItem.getStyle(pCtx), null));
        html.append(">");

        html.append(getValue(pCtx, hItem));
        return html.append("</div>");
    }

    protected String getValue(WebContext pCtx, Text textItem) {
        String value = textItem.getValue();
        if (value != null && value.startsWith("file:")) {
            String fileName = value.substring(5);
            fileName = evaluate(pCtx, textItem, fileName);
            //fileName = pCtx.getWebTop() + fileName;
            
            value = Filer.read(pCtx.getWebTop(),  fileName);
            
        } else if (value != null && value.startsWith("page:")) {
            String pageName = value.substring(5);
            Page page = pCtx.getSession().getPage(pCtx, pageName, true);

            if (!pCtx.isRunTime()) {
                return pageName + " will be displayed here";
            }

            if (page != null) {
                boolean embed = page.isEmbedded();
                page.setEmbedded(true);

                Object sb = page.render(pCtx);
                page.setEmbedded(embed);

                return sb.toString();
            } else {
                return "Page " + pageName + " not found";
            }
        } else {
            if (textItem.isExpression())
                value = evaluate(pCtx, textItem, textItem.getValue());
            else
                value = textItem.getValue();

            if (value == null) {
                return null;
            }
            if (isEmptyString(value) && !pCtx.isRunTime() && 
                textItem.isDesignable()) {
                value = textItem.getValue();

                if (isEmptyString(value)) {
                    value = "&nbsp;";
                }
            }
        }
        if(value==null)
            value = this.evaluate(pCtx, textItem, textItem.getDefaultValue());
        return value;
    }
}